package com.youmingxing.adpter.selfInfo;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.activity.selfInfo.MyAuditionEndActivity;
import com.youmingxing.activity.selfInfo.MyAuditionGoingActivity;
import com.youmingxing.bean.AuditionFromUser;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.ViewHolderUtils;

import org.json.JSONObject;

import java.util.List;

/**
 * 我的海选
 */
public class MyAuditionsAdapter extends BaseAdapter {
    private List<AuditionFromUser> auditionFromUserList;
    private TextView nameTv;
    private TextView cateText;
    private TextView statusT;
    private TextView myticketsTv;
    private TextView rankingTV;
    private ImageView icon;
    private Button quiteBtn;
    private View viewLine;

    private BaseActivity context;


    public MyAuditionsAdapter(BaseActivity mContext, List<AuditionFromUser> auditionFromUserList) {
        this.context = mContext;
        this.auditionFromUserList = auditionFromUserList;
    }

    @Override
    public int getCount() {
        return auditionFromUserList == null ? 0 : auditionFromUserList.size();
    }

    @Override
    public Object getItem(int position) {
        if ((null != auditionFromUserList && auditionFromUserList.size() > 0) && (position >= 0 && position < auditionFromUserList.size())) {
            return auditionFromUserList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_myauditions_item, null);
        }
        RelativeLayout auditionLayout=ViewHolderUtils.getViewHolderView(convertView, R.id
                .audition_total);
        nameTv = ViewHolderUtils.getViewHolderView(convertView, R.id.audition_name);
        cateText = ViewHolderUtils.getViewHolderView(convertView, R.id.category_name);
        statusT = ViewHolderUtils.getViewHolderView(convertView, R.id.audition_status);
        myticketsTv = ViewHolderUtils.getViewHolderView(convertView, R.id.vote_num);
        rankingTV = ViewHolderUtils.getViewHolderView(convertView, R.id.rank_tv);

        icon = ViewHolderUtils.getViewHolderView(convertView, R.id.category_img);
        quiteBtn = ViewHolderUtils.getViewHolderView(convertView, R.id.auditiona_quit_btn);
        viewLine = ViewHolderUtils.getViewHolderView(convertView, R.id.viewline);

        nameTv.setText(auditionFromUserList.get(position).movie);
        cateText.setText(auditionFromUserList.get(position).category);
        myticketsTv.setText(context.getResources().getString(R.string.auditions_num) + auditionFromUserList.get(position).votes);
        rankingTV.setText(context.getResources().getString(R.string.auditions_ranking) + auditionFromUserList.get(position).rank);
        Glide.with(context).load(auditionFromUserList.get(position).category_img)
                .placeholder(R.drawable.default_16_9).error(R.drawable.default_16_9)
                        .crossFade().into(icon);
        if (position == 0) {
            viewLine.setVisibility(View.INVISIBLE);
        } else {
            viewLine.setVisibility(View.VISIBLE);
        }

        statusT.setText(auditionFromUserList.get(position).status_tip);
        switch (auditionFromUserList.get(position).status) {
            case 0:
                statusT.setTextColor(context.getResources().getColor(R.color.text_red));
                statusT.setBackgroundColor(context.getResources().getColor(R.color.background_pink));
                quiteBtn.setVisibility(View.VISIBLE);
                break;
            case 1:
                statusT.setTextColor(Color.BLACK);
                statusT.setBackgroundColor(context.getResources().getColor(R.color.background_pink));
                quiteBtn.setVisibility(View.INVISIBLE);
                break;
            case 2:
                statusT.setTextColor(context.getResources().getColor(R.color.text_red));
                statusT.setBackgroundColor(context.getResources().getColor(R.color.background_pink));
                quiteBtn.setVisibility(View.INVISIBLE);
                break;
            case 3:
                statusT.setTextColor(Color.WHITE);
                statusT.setBackgroundColor(context.getResources().getColor(R.color.background_black));
                quiteBtn.setVisibility(View.INVISIBLE);
                break;
            default:
                break;
        }
        quiteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle(R.string.quit_audition_title).setMessage(R.string.quit_audition_message)
                        .setPositiveButton
                        ("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        AllStarApi.quitAudition(auditionFromUserList.get(position).id, new BaseJsonHandler(context) {
                            @Override
                            public void onSuccessWithData(JSONObject response) {
                                context.showToast("退出海选成功");
                                auditionFromUserList.remove(position);
                                notifyDataSetChanged();
                            }
                        });
                    }
                }).setNegativeButton("否", null).show();


            }
        });
        auditionLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (auditionFromUserList.get(position).status == 0) {
                    intent.setClass(context, MyAuditionGoingActivity.class);
                } else {
                    intent.setClass(context, MyAuditionEndActivity.class);
                }
                intent.putExtra("id", auditionFromUserList.get(position).id);
                context.startActivity(intent);
            }
        });

        return convertView;
    }
}